package edu.sctu.xiangkun.day201506.day20150628;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;


import java.io.*;

/**
 * Created by user04 on 2015/6/28.
 */
public class JsonUtil{
    static HttpClientBuilder builder = HttpClientBuilder.create();
    static CloseableHttpClient client = builder.build();

    public static String GetJson(String path) throws IOException {
        HttpGet get = new HttpGet(path);
        HttpResponse response = client.execute(get);
        HttpEntity entity =response.getEntity();

        InputStream inputStream = entity.getContent();

        String str;
        str = steamToString(inputStream);


        get.releaseConnection();
        return str;
    }
    public static JsonAppbean toJsonString(String json){

        JSONObject jsonObject = JSON.parseObject(json);
        JsonAppbean jsonAppbean = JsonAppbean.getDateObj();

        jsonAppbean.error = jsonObject.getIntValue("error");
        jsonAppbean.message = jsonObject.getString("message");
        jsonAppbean.count = jsonObject.getString("count");


        String list = jsonObject.getString("list");
        JSONArray jsonArray = JSON.parseArray(list);

        for(int i = 0;i<jsonArray.size();i++){
            ListAppbean listAppbean = new ListAppbean();
            listAppbean.rec_id = jsonArray.getJSONObject(i).getString("rec_id");
            listAppbean.goods_id  = jsonArray.getJSONObject(i).getString("goods_id");
            listAppbean.category_id  = jsonArray.getJSONObject(i).getString("category_id");
            listAppbean.brand_id  = jsonArray.getJSONObject(i).getString("brand_id");
            listAppbean.goods_name  = jsonArray.getJSONObject(i).getString("goods_name");
            listAppbean.goods_image  = jsonArray.getJSONObject(i).getString("goods_image");
            listAppbean.goods_price  = jsonArray.getJSONObject(i).getString("goods_price");
            listAppbean.goods_type  = jsonArray.getJSONObject(i).getString("goods_type");
            listAppbean.goods_introduce  = jsonArray.getJSONObject(i).getString("goods_introduce");
            listAppbean.goods_device  = jsonArray.getJSONObject(i).getString("goods_device");
            listAppbean.goods_fictionsale  = jsonArray.getJSONObject(i).getString("goods_fictionsale");
            listAppbean.rec_time  = jsonArray.getJSONObject(i).getString("rec_time");
            listAppbean.rec_top  = jsonArray.getJSONObject(i).getString("rec_top");
            listAppbean.recommend_num  = jsonArray.getJSONObject(i).getString("recommend_num");
            listAppbean.goods_likes  = jsonArray.getJSONObject(i).getString("goods_likes");
            listAppbean.s_goods_device  = jsonArray.getJSONObject(i).getString("s_goods_device");
            listAppbean.s_rec_time  = jsonArray.getJSONObject(i).getString("s_rec_time");
            jsonAppbean.list.add(listAppbean);
        }

        jsonAppbean.page = jsonObject.getIntValue("page");
        jsonAppbean.size = jsonObject.getIntValue("size");

        return jsonAppbean;
    }

    public static void serializableGoods(){
        try {
            ObjectOutputStream outputStream = new ObjectOutputStream(new FileOutputStream(new File("D:/Json//Appbean.txt")));
            outputStream.writeObject(JsonAppbean.getDateObj());
            outputStream.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static String steamToString(InputStream inputStream) throws IOException {
        byte[] buffer = new  byte[1024];
        int count;
        String str = "     ";

        while((count = inputStream.read(buffer)) !=-1){
            str = str + new String(buffer,0,count,"utf-8");
        }
        return str;
    }

    public  static void printfJson() {
        try {
            JsonAppbean jsonAppbean = (JsonAppbean) new ObjectInputStream(new FileInputStream(new File("D://Json//Appbean.txt"))).readObject();
            System.out.println("error:" + jsonAppbean.error);
            System.out.println("message:" + jsonAppbean.message);
            System.out.println("count:" + jsonAppbean.count);

            for (int i = 0; i < jsonAppbean.list.size(); i++){
                System.out.println();
                System.out.println("goods" + (i + 1));
                System.out.println("rec_id:" + jsonAppbean.list.get(i).rec_id);
                System.out.println("goods_id:" + jsonAppbean.list.get(i).goods_id);
                System.out.println("category_id:" + jsonAppbean.list.get(i).category_id);
                System.out.println("brand_id:" + jsonAppbean.list.get(i).brand_id);
                System.out.println("goods_name:" + jsonAppbean.list.get(i).goods_name);
                System.out.println("goods_image:" + jsonAppbean.list.get(i).goods_image);
                System.out.println("goods_price:" + jsonAppbean.list.get(i).goods_price);
                System.out.println("goods_type:" + jsonAppbean.list.get(i).goods_type);
                System.out.println("goods_introduce:" + jsonAppbean.list.get(i).goods_introduce);
                System.out.println("goods_device:" + jsonAppbean.list.get(i).goods_device);
                System.out.println("goods_fictionsale:" + jsonAppbean.list.get(i).goods_fictionsale);
                System.out.println("rec_time:" + jsonAppbean.list.get(i).rec_time);
                System.out.println("rec_top:" + jsonAppbean.list.get(i).rec_top);
                System.out.println("recommend_num:" + jsonAppbean.list.get(i).recommend_num);
                System.out.println("goods_likes:" + jsonAppbean.list.get(i).goods_likes);
                System.out.println("s_goods_device:" + jsonAppbean.list.get(i).s_goods_device);
                System.out.println("s_rec_time:" + jsonAppbean.list.get(i).s_rec_time);
            }
            System.out.println("page:" + jsonAppbean.page);
            System.out.println("size:" + jsonAppbean.size);

        } catch (IOException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();

        }
    }
}
